Ext.namespace("MyPlan");
/*
MyPlan.plans = {
	marketweekly: null,
	groupweekly: null,
	adminweekly: null,
	busiweekly: null,
	adminmonthly: null
};
MyPlan.initPlan = {
	'1': function(p) {
		if (MyPlan.plans.marketweekly == null) {
			MyPlan.plans.marketweekly = new MyPlan.MarketWeekly(p);
		}
		return MyPlan.plans.marketweekly;
	},
	'2': function(p) {
		if (MyPlan.plans.groupweekly == null) {
			MyPlan.plans.groupweekly = new MyPlan.GroupWeekly(p);
		}
		return MyPlan.plans.groupweekly;
	},
	'3': function(p) {
		if (MyPlan.plans.adminweekly == null) {
			MyPlan.plans.adminweekly = new MyPlan.AdminWeekly(p);
		}
		return MyPlan.plans.adminweekly;
	},
	'4': function(p) {
		if (MyPlan.plans.busiweekly == null) {
			MyPlan.plans.busiweekly = new MyPlan.BusiWeekly(p);
		}
		return MyPlan.plans.busiweekly;
	},
	'5': function(p) {
		if (MyPlan.plans.adminmonthly == null) {
			MyPlan.plans.adminmonthly = new MyPlan.AdminMonthly(p);
		}
		return MyPlan.plans.adminmonthly;
	},
};*/

MyPlan.New = function(p) {
	this.winMain = p;
	this.winItem=null;
	this.temp=0;
	this.txtState=new Ext.form.Hidden({name:'planState'});
	this.txtRemind=new Ext.form.Hidden({name:'remind'});	
	
	this.planName = new Ext.form.TextField({
		fieldLabel: '计划名称',
		allowBlank: false,
		name:'planName',
		width: 300
	});
	this.typeStore  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: [[1, '周计划'], [2, '月计划'], [5, '年度计划']]
    });
	//this.typeStore.load();
	this.planType = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '计划类型',
		name:'planType',
		store: this.typeStore,
		value:"1",	
        valueField: 'value',
        displayField: 'name',
        editable: false,
        allowBlank: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	//this.planType.on('select', this.changeType, this);	
	//this.planType = new Ext.form.Hidden({name:'planType'});
	
	this.auditorStore=new Ext.data.JsonStore({
	    autoDestroy: true,
	    root:'auditors',
	    url: '../myplan/selectAuditors.form?timesnap=' + Date.parse(new Date()),
	    fields: ['userId', 'userName' ]
	});
	this.auditorStore.load();
	this.auditorId = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '审核人',
		name:'auditorId',
		store:this.auditorStore,
        valueField: 'userId',
        displayField: 'userName',
        editable: false,
        allowBlank: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
    this.startDate = new Ext.form.DateField({
    	fieldLabel: '计划时间',
    	name:'startDate',
    	allowBlank: false,
		format: 'Y-m-d'
	});
    this.endDate = new Ext.form.DateField({
    	fieldLabel: '～',
    	name:'endDate',
    	allowBlank: false,
    	labelWidth: 20,
    	labelAlign: 'center',
    	labelSeparator: '',
		format: 'Y-m-d'
	});
    
	this.form = new Ext.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        width: 400,
        border: false,
        labelWidth: 70,
        labelAlign: 'right',
        items: [this.planName,this.txtState,this.txtRemind, 
	        { layout: 'column', border: false, defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .5, items: [this.planType]},
		        	            { columnWidth: .5, items: [this.auditorId] } ]
		    },
	        { layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .45, items: [this.startDate]},
	        	            { columnWidth: .55, items: [this.endDate] } ]
	    }]
    });    
	this.fileForm = new Ext.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        width: 400,
        fileUpload: true, 
        border: false,
        labelWidth: 70,
        labelAlign: 'right',
        items: []
	});
	MyPlan.New.superclass.constructor.call(this, {
		title: '新建计划',
		autoHeight: true,
        width: 420,
        height:400,
        y: 50,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form,this.fileForm],	
		buttons:[{
			text: '下一步',
			cls: 'x-btn-text-icon btn-next',
			handler: this.onNext,
			scope: this
        },/*{
			text: '添加附件',
			iconCls: 'icon-new',
			handler: this.addFile,
			scope: this
        },*/{
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
    });
};
Ext.extend(MyPlan.New, Ext.Window,{
	initWin:function(){
		this.form.getForm().reset();
		//this.fileForm.removeAll(true);		
		var conn = new Ext.data.Connection({
			url:'../myplan/initPlan.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		})
		Utils.request(conn,{
			params:{},
			scope:this,
			success:function(data){
				this.scope.startDate.setValue(data.result.START_DATE);
				this.scope.endDate.setValue(data.result.END_DATE);
				this.scope.planName.setValue(data.result.planName);
				this.scope.planType.setValue(data.result.planType);
				this.scope.auditorId.setValue(data.result.auditorId);
			}
		});
		//this.addFile();
	},
	addFile:function(o){
		this.temp+=1;
		var fname="fileName"+this.temp;
		var fileField=new Ext.form.FileUploadField({
	    	fieldLabel: '上传附件',
			emptyText: '请选择要上传的文件',
			width : 180,
			name : fname,
			buttonText: '',
			buttonCfg: {
	            iconCls: 'icon-compress'
	        }
	    });
		var fileDel=new Ext.Button({
	    	text: '清除',
	    	iconCls: 'icon-remove',
	    	scope: this,
	    	handler: this.delFile
	    });
		var item={ layout: 'column', border: false, 
					defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .85, items: [fileField]},
		        	         { columnWidth: .15, items: [fileDel] }]
		    	};
		    	
		this.fileForm.add(item);
	    this.fileForm.doLayout();
	},
	delFile:function(btn,event){
		var item=btn.ownerCt.ownerCt;
		this.fileForm.remove(item,true);
	    this.fileForm.doLayout();
	},
	/*
	changeType: function(c, r, i) {
		if (this.planName.getValue() === '') {
			this.planName.setValue(this.getPlanName(r.data.type));
		}
		var dateRange = Utils.getNextDate(r.data.type);
		this.startDate.setValue(dateRange[0]);
		this.endDate.setValue(dateRange[1]);
	},*/
	onNext: function(btn, event) {
		/*var planType = this.planType.getValue();
		if (!MyPlan.initPlan[planType]) {
			Utils.info('无效计划类型，请联系管理员!');
			return;
		}
		this.winPlan = MyPlan.initPlan[planType](this);*/
		if (!this.form.getForm().isValid()) return;
		if (this.winItem == null) {
			this.winItem = new MyPlan.Weekly(this);
		}
		/*this.winItem.initWin();
        this.winItem.show();
		this.hide();*/
		if (this.startDate.getValue()>=this.endDate.getValue()){
			Utils.warn2("计划开始时间应小于结束时间！",240);
			return;
		}		
		//判断本周期内有无计划
		var conn = new Ext.data.Connection({
			url:'../myplan/checkPlan.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		});
		var param=this.form.getForm().getValues();
		Utils.request(conn,{
			params:param,
			scope:this,
			success:function(data){
				if(data.result>0){
					Utils.warn2("该时间段内已存在计划，不允许新增计划！",240);					
				}else{
			        this.scope.winItem.initWin();
			        this.scope.winItem.show();
					this.scope.hide();
				}
			}
		});			
	}
});
